package com.google.collabframework.texteditor.server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author robert
 */
public class LoginServer extends UnicastRemoteObject implements ILoginServer {

    private int clientCount;

    public LoginServer() throws RemoteException {
        super();
    }

    public ITextServer registerClient() throws RemoteException {
        clientCount++;
        
        try {
            Naming.rebind("TextServer" + clientCount, new TextServer());
            return (ITextServer) Naming.lookup("//localhost/TextServer" + clientCount);
        } catch (NotBoundException ex) {
            Logger.getLogger(LoginServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(LoginServer.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public static void main(String[] args) {
        try {
            LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
            Naming.rebind("Login", new LoginServer());
        } catch (MalformedURLException ex) {
            Logger.getLogger(LoginServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(LoginServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
